<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller 
{
	function __construct()
	{
		parent::__construct();
		
		//load helper
		$this->load->helper('login_helper');
		
			//load model

		$this->load->model('Muser');
		


    //load libraries

    $this->load->library('pagination');

    $this->load->library('form_validation');
	// session
	 session_start();
		 if ( !isset($_SESSION['row_number']))

			$_SESSION['row_number']= 12;
			
	// check login
	check_login();
	load_lang("admin");
	}
	function adm()
	{
		$data  = array ("1123","Dsf","tuong");
		$s = serialize($data);
		echo $s;
		$data  = array ("vi"=>"1123","cn"=>"Dsf","en"=>"tuong");
		$s2 = serialize($data);
		echo "<br />".$s2;
		$f=unserialize($s2);
		print_r($f);
	}

	function index($page='')
	{
	
		$limit = $_SESSION['row_number'];

		$config['base_url'] = site_url().'/admin/user/index/';

		$config['first_link'] = 'First';

		$config['last_link'] = 'Last';

		$config['next_link'] = '&gt;&gt;';

		$config['prev_link'] = '&lt;&lt;';

		$config['cur_tag_open'] = '<a href="#" class="current">';

		$config['cur_tag_close'] = '</a>';

		$config['uri_segment'] = 4;

		$config['num_links'] = 2;

		$config['total_rows'] = $this->Muser->count_all();

		$config['per_page'] = $limit;
		

		$this->pagination->initialize($config);

    if (empty($page))	$page = 0; //default value

    $data['result'] = $this->Muser->list_data($page,$limit);// $this->Muser->user_review($page, $limit);

    $data['title'] = $this->lang->line('title_user_review');

    $data['page'] = str_replace('&nbsp;', '', $this->pagination->create_links());

    $data['main'] = 'user_review';
	


    $this->load->view('admin/layout/template_main', $data);
	}
	
	
	
	//***********update  *************//
	function sub_update()
	{
		$id=$this->uri->segment(4);
			if(isset($_POST["update"]))
		{//$this->Muser->check_email($this->input->post("email"),$this->uri->segment(4))
			if(!$this->Muser->check_username($this->input->post("user_name"),$this->uri->segment(4)))
			{	
				$this->form_validation->_error_array["user_name"] = "This Username have been existed ."; 
			
				return false;
			
			}
			$name = array('first_name'=> htmlspecialchars($this->input->post('first_name'),ENT_QUOTES,'UTF-8'),'last_name'=>htmlspecialchars( $this->input->post('last_name'),ENT_QUOTES,'UTF-8'));
	$name = serialize($name);
			if(isset($_POST["user_pass"]) && $_POST["user_pass"] != "")
			
			$data = array(
				"user_name" => $this->input->post('user_name'),

				"user_pass" => pass_md5($this->input->post("user_pass")),
				"user_member" => $name,
			  "user_status"=> $this->input->post("user_status"),
			/*	"birthday" => $this->input->post("birthday"),
				"phone" => $_POST["phone"],
				"sex" => $_POST["sex"],
				"address" => $_POST["address"],
				*/);
			else
				$data = array(
				"user_name" => $_POST["user_name"],
		//		"user_lvl" => $_POST["permission"],
			"user_member" => $name,
			  "user_status"=> $this->input->post("user_status"),
			);
			
			//print_r($data);
			//echo $this->input->post('user_name');
			if($this->Muser->update($data,$id))
				return true;
			else
				return false;
		}
		else
			return false;
	
	
	}
	
	function update()
	{
		$id= $this->uri->segment(4);
	 if ($this->input->post('update'))

    {
	
		
	
$this->form_validation->set_rules('user_name', 'Username', 'required|min_length[5]|callback_check_user');
$this->form_validation->set_rules('last_name', 'last_name', 'required');

      $this->form_validation->set_rules('first_name', 'first_name', 'required');
    
    // $this->form_validation->set_rules('phone', 'phone', 'required|numeric');
		  if ($this->form_validation->run() && $this->sub_update())

        redirect('admin/user/', 'refresh');

      else
		{
			
        	$data['missing'] = $this->form_validation->_error_array;
			
		}

    }

    elseif ($this->input->post('close'))

		{

			redirect('admin/user/', 'refresh');

		}
		
			 //reload template

		
		$data['result']= $this->Muser->info_form_id($id);
		  $data['title'] =$this->lang->line('title_user_update');
		    $data['main'] = 'user_update';
		//		$data['per'] = $this->Muser->get_per();
			  $this->load->view('admin/layout/template_main', $data);
		
	}
	function create()
	{
		//print_r($_POST);
	//	print_r($this->input->post());
	//	echo $this->input->post('user_name');
	 if($this->input->post('create')  )

    {
//	echo 'qwe';
	
$this->form_validation->set_rules('user_name', 'Username', 'required|min_length[6]');
      $this->form_validation->set_rules('user_pass', 'password', 'required');
$this->form_validation->set_rules('last_name', 'last_name', 'required');

      $this->form_validation->set_rules('first_name', 'first_name', 'required');
      

      if ($this->form_validation->run() && $this->sub_create())

        redirect('admin/user/', 'refresh');

      else
		{
			
        	$data['missing'] = $this->form_validation->_error_array;
			
		}

    }

    elseif ($this->input->post('close'))

		{

			redirect('admin/user/', 'refresh');

		}



	
		$data['main'] = 'user_create';
		
	//		$data['per'] = $this->Muser->get_per();

    $data['title'] = $this->lang->line('title_user_create');

		$this->load->view('admin/layout/template_main', $data);
	}
	
	public function check_user($username){

	//	$this->load->Model("admin/Muser");

		$id=$this->uri->segment(4);

		if($id != ""){

			$data=$this->Muser->check_username($username,$id); 

		}else{

			$data=$this->Muser->check_username($username);

		}

		if($data == FALSE)

		{

			$this->form_validation->set_message("check_user","Your username has been register, please try again");

			return FALSE;

		}else{

			return TRUE;			

		}

	}
	
	
	function check_email()
	{
		$email=$this->uri->segment(4);
		if(!$this->Muser->check_email($email))
			echo "1";
		else
			echo "0";

	}
	
	function sub_create()
	{
		if(isset($_POST["create"]))
		{
	/*	$email=$this->input->post("email");
			if($this->Muser->check_email($this->input->post("email")) )
			{	
				$this->form_validation->_error_array["email"] = "This Email have been existed ."; 
			
				return false;
			
			}
			
	*/	
	$name = array('first_name'=> $this->input->post('first_name'),'last_name'=> $this->input->post('last_name'),);
	$name = serialize($name);
	$data = array(
			"user_name" => $this->input->post("user_name"),
		
			"user_pass" => pass_md5($this->input->post("password")),
			"user_member" => $name,
			  "user_status"=> $this->input->post("user_status"),
		
			);
	
	
			
		if($this->Muser->insert($data))
			return true;
		else
			return false;
		}
		else
			return false;
		
		
	}
	
	// search 
	
	 function search()

	{

		$keyword = $this->input->post('keyword');

		if ( ! empty($keyword))

		{

			$data['keyword'] = $keyword;

     	 $data['search_type'] = $this->input->post('search_type');

			$data['result'] = $this->Muser->search();



      //reload template

			$data['main'] = 'user_search';

      $data['title'] = $this->lang->line('title_user_search');

			$this->load->view('admin/template_main', $data);

		}

		else

		{

			redirect('admin/user/', 'refresh');

		}

	}

	
	
	
	function request()

	{

		
		$require = array('show', 'delete');

		foreach ($_POST as $key=>$value)

		{
		
			if (in_array($key, $require))

			{

				$act = $key;
				
			}
			}



		//process 'action'

		if (! empty($act))

		{

			switch ($act)

			{

				case 'show': //show

				{
				
					$_SESSION['row_number'] = $this->input->post('row_number');
					
				} break;



				case 'delete': //delete

				{
				
					$this->Muser->delete();

				} break;

			}//end switch



			redirect('admin/user/', 'refresh');

		}//end if

	}
	public function delete($id)
	{
		if($id >0 )
		
			$this->Muser->del_item($id);
		redirect('admin/user/', 'refresh');
		
	}
	
}









?>
